package com.xjm.bean.init;

import com.xjm.model.Person;
import com.xjm.model.PersonFactory;
import com.xjm.model.PersonStaticFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @author jaymin<br>
 * 理解BeanDefinition的内容<br>
 * 2020/11/28 0:20
 */
@SuppressWarnings("all")
public class BeanDefinitionDemoByXml {
	public static void main(String[] args) {
		// 1. 声明配置类路径
		String xmlPath = "D:\\Spring\\spring-framework-5.1.x\\spring-demo\\src\\main\\resources\\spring\\spring-config.xml";
		// 2. 加载xml配置类
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
		System.out.println("****************Before Spring********************");
		// 默认的构造函数
		Person person = new Person();
		System.out.println(person.toString());
		// 使用静态方法创建Person
		Person kobe = PersonStaticFactory.getPerson();
		System.out.println(kobe.toString());
		// 使用工厂创建Person
		PersonFactory personFactory = new PersonFactory();
		System.out.println(personFactory.getPerson().toString());
		System.out.println("****************Using Spring********************");
		// 默认构造函数创建Bean
		Person personA = (Person) applicationContext.getBean("personA");
		// 通过静态工厂方法创建Bean
		Person personB = (Person) applicationContext.getBean("personB");
		// 通过工厂方法创建Bean
		Person personC = (Person) applicationContext.getBean("personC");
		System.out.println(personA.toString()+"hashcode:"+personA.hashCode());
		System.out.println(personB.toString()+"hashcode:"+personB.hashCode());
		System.out.println(personC.toString()+"hashcode:"+personC.hashCode());
		System.out.println("************Singleton*********************");
		Person personAA = (Person) applicationContext.getBean("personA");
		Person personBB = (Person) applicationContext.getBean("personB");
		Person personCC = (Person) applicationContext.getBean("personC");
		System.out.println(personAA.toString()+"hashcode:"+personAA.hashCode());
		System.out.println(personBB.toString()+"hashcode:"+personBB.hashCode());
		System.out.println(personCC.toString()+"hashcode:"+personCC.hashCode());
	}
}
